# -*- coding: utf-8 -*-
# @Time    : 2024/11/18 12:53
# @Author  : wangwei
# @File    : demo.py
import jsonpath
import pytest
import pytest_assume
import requests


class RequestsClientDome:
    # 使用类属性来定义个session，他将作为所有接口发起的全局对象
    session = requests.session()

    def __init__(self):
        self.session = RequestsClientDome.session
        self.headers = None
        self.method = None
        self.data = None
        self.params = None
        self.json = None
        self.files = None
        self.url = None
        self.resp = None
        self.apiname=None




    # 表示接口调用
    def send(self, **kwargs):
        print(f"============================【{self.apiname}】执行开始了================================")
        # 注意：并不是所有接口是data参数，也可能是其他参数
        # 判断下接口请求的各项信息，在kwargs中有没有，如果没有就用对象自身各个属性
        if 'url' not in kwargs:
            kwargs['url'] = self.url
        if 'method' not in kwargs:
            kwargs['method'] = self.method
        if 'headers' not in kwargs:
            kwargs['headers'] = self.headers
        if 'data' not in kwargs:
            kwargs['data'] = self.data
            # kwargs['data'] = self.aes.encrypt(self.data)
            # kwargs['data'] = self.rsa.encrypt_data(self.data)
        if 'json' not in kwargs:
            kwargs['json'] = self.json
            # kwargs['json'] = self.aes.encrypt(self.json)
            # kwargs['json'] = self.rsa.encrypt_data(self.json)
        if 'params' not in kwargs:
            kwargs['params'] = self.params
            # kwargs['params'] = self.aes.encrypt(self.params)
            # kwargs['params'] = self.rsa.encrypt_data(self.params)
        if 'files' not in kwargs:
            kwargs['files'] = self.files
        # 记录接口相关的请求信息
        for key, value in kwargs.items():
            print(f'接口的{key}是:{value}')
        try:
            self.resp = self.session.request(**kwargs)
            print(f'接口响应状态码:{self.resp.status_code}')
            print(f'接口响应body信息:{self.resp.text}')
            # print(f'接口响应body信息【json】:{self.resp.json()}')
        except BaseException as e:
            print('接口调用异常')
            raise print(f'接口调用异常:{e}')
        finally:
            print(f"============================【{self.apiname}】执行结束了================================")
        return self.resp



class DomeTest(RequestsClientDome):
    def __init__(self):
        super().__init__()
        self.method="GET"
        self.url="http://apis.juhe.cn/xzpd/query"
        self.params={
                "men":"狮子",
                "women": "白羊",
                "key": "c6aa6f082901c4b7fb6e3cc2eac98b72"
            }



def test_demo():
    res_json=DomeTest().send().json()
    print(res_json)
    women=jsonpath.jsonpath(res_json,"$..women")[0]
    print(women)
    # 使用 pytest.assume 进行断言
    pytest.assume(res_json.get("result").get("men") == "狮子", "测试失败: men 不等于 狮子")
    pytest.assume(women == "白羊", "测试失败: women 不等于 白羊")
